package com.have.blog.web.controller;

import com.have.blog.base.wrapper.CommonPage;
import com.have.blog.base.wrapper.Wrapper;
import com.have.blog.web.dto.BlogDto;
import com.have.blog.web.service.WebBlogService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

@RestController
@RequestMapping("/search")
@Api(value = "SQL搜索相关接口", tags = {"SQL搜索相关接口"})
@Slf4j
public class BlogSearchController {

    @Autowired
    private WebBlogService blogService;

//    @BussinessLog(value = "搜索Blog", behavior = EBehavior.BLOG_SEARCH)
    @ApiOperation(value = "搜索Blog", notes = "搜索Blog")
    @GetMapping("/sqlSearchBlog")
    public Wrapper sqlSearchBlog(@ApiParam(name = "keywords", value = "关键字", required = true) @RequestParam(required = true) String keywords,
                                 @ApiParam(name = "pageNum", value = "当前页数", required = false) @RequestParam(name = "currentPage", required = false, defaultValue = "1") Integer pageNum,
                                 @ApiParam(name = "pageSize", value = "每页显示数目", required = false) @RequestParam(name = "pageSize", required = false, defaultValue = "10") Integer pageSize) {

        if (StringUtils.isEmpty(keywords) || StringUtils.isEmpty(keywords.trim())) {
            return Wrapper.illegal();
        }
        List<BlogDto> resultList = blogService.getBlogByKeyword(pageNum, pageSize, keywords);
        return Wrapper.success(CommonPage.restPage(resultList));
    }

//    @BussinessLog(value = "根据作者获取相关的博客", behavior = EBehavior.BLOG_AUTHOR)
    @ApiOperation(value = "根据作者获取相关的博客", notes = "根据作者获取相关的博客")
    @GetMapping("/searchBlogByAuthor")
    public Wrapper searchBlogByAuthor(HttpServletRequest request,
                                     @ApiParam(name = "author", value = "作者名称", required = true) @RequestParam(name = "author", required = true) String author,
                                     @ApiParam(name = "pageNum", value = "当前页数", required = false) @RequestParam(name = "pageNum", required = false, defaultValue = "1") Integer pageNum,
                                     @ApiParam(name = "pageSize", value = "每页显示数目", required = false) @RequestParam(name = "pageSize", required = false, defaultValue = "10") Integer pageSize) {
        if (StringUtils.isEmpty(author)) {
            return Wrapper.failed("作者不能为空");
        }
        List<BlogDto> resultList = blogService.searchBlogByAuthor(author, pageNum, pageSize);
        return Wrapper.success(CommonPage.restPage(resultList));
    }
}
